package com.cabrida.livestar.main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.mainstar.CircleActivity;
import com.cabrida.livestar.model.FansCircleMsgChildrenData;
import com.cabrida.livestar.model.FansCircleMsgData;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;
import com.cabrida.livestar.view.ListViewExtend;

/**
 * 圈子评论adapter
 * 
 * @author why
 * 
 */
public class FansCircleMsgLvAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<FansCircleMsgData> data_list = new ArrayList<FansCircleMsgData>();

	public FansCircleMsgLvAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
	}

	public void setData(ArrayList<FansCircleMsgData> data) {
		data_list = data;
	}

	@Override
	public int getCount() {
		return data_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data_list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(
					R.layout.adapter_circlemsg_lv_item, null);
			
			holder.iv_header = (CircleImageView) convertView.findViewById(R.id.iv_header);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_lv = (TextView) convertView.findViewById(R.id.tv_lv);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
			holder.lv_children = (ListViewExtend) convertView.findViewById(R.id.lv_children);
			holder.childrenAdapter = new ChildrenAdapter(mContext);
			holder.lv_children.setAdapter(holder.childrenAdapter);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		
		FansCircleMsgData data = data_list.get(position);
		
		MyUtils.loadImg(data.getU_avatar(), holder.iv_header);
		holder.tv_name.setText(data.getU_nickname().toString());
//		holder.tv_lv.setText(data.getU_nickname().toString());
		holder.tv_time.setText(data.getC_createtime());
		holder.tv_content.setText(data.getC_content());
		holder.childrenAdapter.setData(data.getChildrenList());
		holder.childrenAdapter.notifyDataSetChanged();
		holder.lv_children.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				FansCircleMsgChildrenData childrenData = (FansCircleMsgChildrenData) arg0.getAdapter().getItem(arg2);
				intent.setAction(CircleActivity.ACTION_CHILDREN_MSG);
				intent.putExtra("data", childrenData);
				mContext.sendBroadcast(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		private CircleImageView iv_header;
		private TextView tv_name;
		private TextView tv_lv;
		private TextView tv_time;
		private TextView tv_content;
		private ChildrenAdapter childrenAdapter;
		private ListViewExtend lv_children;
	}
	
	 public class ChildrenAdapter extends BaseAdapter {
	        private LayoutInflater mInflater;
	        private ArrayList<FansCircleMsgChildrenData> data_list = new ArrayList<FansCircleMsgChildrenData>();

	        public ChildrenAdapter(Context context) {
	            this.mInflater = LayoutInflater.from(context);
	        }

	        public void setData(ArrayList<FansCircleMsgChildrenData> data) {
	            data_list = data;
	        }

	        @Override
	        public int getCount() {
	            return data_list.size();
	        }

	        @Override
	        public Object getItem(int arg0) {
	            return data_list.get(arg0);
	        }

	        @Override
	        public long getItemId(int arg0) {
	            return arg0;
	        }

	        @Override
	        public View getView(int position, View convertView, ViewGroup parent) {
	            ViewHolder holder = null;
	            if (convertView == null) {
	                holder = new ViewHolder();
	                convertView = mInflater.inflate(R.layout.view_msg_children, null);
	                holder.tv_replier = (TextView) convertView.findViewById(R.id.tv_replier);
	                holder.tv_replyto = (TextView) convertView.findViewById(R.id.tv_replyto);
	                holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
	                holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
	                convertView.setTag(holder);
	            } else {
	                holder = (ViewHolder) convertView.getTag();
	            }
	            FansCircleMsgChildrenData childrenData = data_list.get(position);
	            holder.tv_replier.setText(childrenData.getCnickname().toString());
	            holder.tv_replyto.setText(childrenData.getPnickname().toString());
	            holder.tv_time.setText(childrenData.getC_createtime().toString());
	            holder.tv_content.setText(childrenData.getC_content().toString());
	            return convertView;
	        }

	        class ViewHolder {
	            private TextView tv_replier;
	            private TextView tv_replyto;
	            private TextView tv_time;
	            private TextView tv_content;
	        }

	    }
	
}
